import {http} from '@/utils/http/axios';

export interface BasicResponseModel<T = any> {
  code: number;
  message: string;
  result: T;
}

export interface BasicPageParams {
  pageNumber: number;
  pageSize: number;
  total: number;
}

/**
 * @description: 获取用户信息
 */
export function getUserInfo(params) {
  return http.request({
    url: '/admin_info',
    method: 'get',
    params: params
  });
}

/**
 * @description: 用户登录
 */
export function login(params) {
  return http.request<BasicResponseModel>(
    {
      url: '/login',
      method: 'POST',
      data: params,
      headers: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        accept: 'application/json, text/plain, */*',
      },
      transformRequest: [
        function (data) {
          let ret = '';
          for (const it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
          }
          return ret;
        },
      ],
    },
    {
      isTransformResponse: false,
      formatDate: true
    }
  );
}

/**
 * @description: 用户修改密码
 */
export function userUpdate(data) {
  return http.request(
    {
      url: `/user/update`,
      method: 'POST',
      data,
    },
    {
      isTransformResponse: false,
    }
  );
}

/**
 * @description: 用户修改密码
 */
export function changePassword(params, uid) {
  return http.request(
    {
      url: `/user/u${uid}/changepw`,
      method: 'POST',
      params,
    },
    {
      isTransformResponse: false,
    }
  );
}

/**
 * @description: 用户登出
 */
export function logout(params) {
  return http.request({
    url: '/login/logout',
    method: 'POST',
    params,
  });
}

/**
 * @description: 管理员获取用户列表
 */
export function getUserList(params) {
  return http.request({
    url: '/sys/user/list',
    method: 'get',
    params: params
  });
}

/**
 * @description: 管理员修改用户信息
 */
export function sysUserUpdate(data) {
  return http.request(
    {
      url: `/sys/user/update`,
      method: 'POST',
      data,
    },
    {
      isTransformResponse: false,
    }
  );
}

/**
 * @description: 管理员修改用户信息
 */
export function sysUserAdd(data) {
  return http.request(
    {
      url: `/sys/user/add`,
      method: 'POST',
      data,
    },
    {
      isTransformResponse: false,
    }
  );
}

/**
 * @description: 管理员删除用户信息
 */
export function sysUserDel(data) {
  return http.request(
    {
      url: `/sys/user/del`,
      method: 'POST',
      data,
    },
    {
      isTransformResponse: false,
    }
  );
}
